#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,y,x) for (int i=(y);i>=(x);i--)
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define templ template<typename T>
templ bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ bool chkmax(T &x,T y){return x<y?x=y,1:0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ T rnd(T l,T r){return uniform_int_distribution<T>(l,r)(rng);}

typedef long long ll;
#define sz 10100100
#define mod 998244353ll
ll ksm(ll x,int y){ll res=1;for (;y;y>>=1,x=x*x%mod) if (y&1) res=res*x%mod;return res;}
ll inv(ll x){return ksm(x,mod-2);}

struct frac {
    ll a,b; // a/b
    frac(ll x){a=x,b=1;}
    frac(ll x,ll y){a=x,b=y;}
    const frac operator * (const frac &x) const {return frac(a*x.a,b*x.b);}
    const frac operator + (const frac &x) const {return frac(a*x.b+x.a*b,b*x.b);}
    const bool operator < (const frac &x) const {
        ll left=a*x.b,right=b*x.a;
        return left<right;
    }
    const bool operator <= (const frac &x) const {
        ll left=a*x.b,right=b*x.a;
        return left<=right;
    }
    const bool operator > (const frac &x) const {
        ll left=a*x.b,right=b*x.a;
        return left>right;
    }
    const bool operator >= (const frac &x) const {
        ll left=a*x.b,right=b*x.a;
        return left>=right;
    }
};

struct pt {
    ll x,y;
    pt(ll X=0,ll Y=0){x=X,y=Y;}
    const pt operator + (const pt &a) const {return pt(x+a.x,y+a.y);}
    const pt operator - (const pt &a) const {return pt(x-a.x,y-a.y);}
};
ll Cross(const pt &a,const pt &b) {
    return a.x*b.y-a.y*b.x;
}

bool check(pt back,pt _back,frac cur) {
    back=back-_back;
    return frac(back.x)*cur+frac(back.y)<=frac(0);
}

void work() {
    static int vis[sz];
    int n; ll a0,A,B,C,M;
    cin>>n>>a0>>A>>B>>C>>M;
    rep(i,0,M+10) vis[i]=0;
    rep(i,1,n) a0=(A*a0%M*a0%M+B*a0%M+C)%M+1,vis[a0]++;
    int n1=0,n2=0;
    rep(i,1,M) n1+=(vis[i]>0),n2+=(vis[i]>1);
    if (n1*2>=2*(n1-1)+n2) {
        printf("%.6lf %lld\n",1.0*n1/(2*(n1-1)+n2),n1*inv(2*(n1-1)+n2)%mod);
        return;
    }
    frac cur=frac(n1,2*(n1-1)+n2);
    static int w[sz],pre[sz]; int m=0;
    rep(i,1,M) if (vis[i]>0) w[++m]=(vis[i]>1);
    rep(i,1,m) pre[i]=pre[i-1]+w[i];
    int R=m;
    deque<pair<pt,int>>q;
    drep(i,m,1) {
        while (233) {
            int _k=-(pre[R]-pre[i-1])-2*(R-i),_b=R-i+1;
            if (frac(_k)*cur+_b<frac(0)) cur=frac(_b,-_k);
            if (!q.size()) break;
            while ((int)q.size()>=2&&check(q.back().fir,q[(int)q.size()-2].fir,cur)) q.pop_back();
            int L=q.back().sec;
            if (frac(R-L+1)>=frac(pre[R]-pre[L-1]+2*(R-L)+2)*cur) {
                R=L-1;
                q.pop_back();
            }
            else break;
        }
        pt p(2*i+pre[i-1],-i);
        while ((int)q.size()>1&&Cross(q[0].fir-p,q[1].fir-p)>=0)
            q.pop_front();
        q.push_front(MP(p,i));
    }
    printf("%.5lf %lld\n",1.0*cur.a/cur.b,cur.a*inv(cur.b)%mod);
}

int main() {
	freopen("guess.in","r",stdin);
	freopen("guess.out","w",stdout);
    int T; cin>>T;
    while (T--) work();
    return 0;
}